package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.erp.domain.*;
import org.example.erp.service.WarehouseAreaService;
import org.example.erp.service.WarehouseLocationService;
import org.example.erp.service.WarehouseService;
import org.example.erp.service.impl.WmItemRecptLineServiceImpl;
import org.example.erp.service.impl.WmProductRecptLineServiceImpl;
import org.example.erp.service.impl.WmProductRecptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class WmProductRecptContrller {
    @Autowired
    private WmProductRecptServiceImpl wmProductRecptService;
    @Autowired
    private WmProductRecptLineServiceImpl wmProductRecptLineService;

    @Autowired
    WarehouseService warehouseService;
    @Autowired
    WarehouseLocationService warehouseLocationService;
    @Autowired
    WarehouseAreaService warehouseAreaService;


    @GetMapping("/reversoContext/find")
    public DataResult find(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam (required = false)String recptCode,
                           @RequestParam (required = false)String recptName,
                           @RequestParam (required = false)String recptDate){

        return DataResult.success(wmProductRecptService.find(currentPage,pageSize,recptCode,recptName,recptDate));
    }


    @PostMapping("/reversoContext/insert")
    public DataResult insert(@RequestBody WmProductRecpt wmItemRecpt){
        return DataResult.success(wmProductRecptService.save(wmItemRecpt));
    }


    @PutMapping("/reversoContext/update")
    public DataResult update(@RequestBody WmProductRecpt wmItemRecpt){
        return DataResult.success(wmProductRecptService.updateById(wmItemRecpt));
    }

    @DeleteMapping("/reversoContext/delete")
    public DataResult delete(@RequestBody List<Integer> logIds){
        return DataResult.success( wmProductRecptService.removeByIds(logIds));
    }



    @GetMapping("/line/findline")
    public DataResult findline(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(required = false) String recptCode
    ) {
        IPage<WmProductRecptLine> wmProductRecptLineIPage = wmProductRecptService.find(currentPage, pageSize, recptCode);
        return DataResult.success(wmProductRecptLineIPage);
    }


    @PostMapping("/line/insertline")
    public DataResult insert(@RequestBody WmProductRecptLine wmProductRecptLine){
        QueryWrapper<Warehouse> q1=new QueryWrapper<>();
        q1.lambda().eq(Warehouse::getWarehouseId,wmProductRecptLine.getWarehouseId());
        Warehouse one = warehouseService.getOne(q1);
        QueryWrapper<WarehouseArea> q2=new QueryWrapper<>();
        q2.lambda().eq(WarehouseArea::getWarehouseAreaId,wmProductRecptLine.getAreaId());
        WarehouseArea one1 = warehouseAreaService.getOne(q2);
        QueryWrapper<WarehouseLocation> q3=new QueryWrapper<>();
        q3.lambda().eq(WarehouseLocation::getWarehouseLocationCode,wmProductRecptLine.getLocationCode());
        WarehouseLocation one2 = warehouseLocationService.getOne(q3);
        wmProductRecptLine.setWarehouseCode(one.getWarehouseCode());
        wmProductRecptLine.setAreaCode(one1.getWarehouseAreaCode());
        wmProductRecptLine.setLocationId(one2.getWarehouseLocationId());
        return DataResult.success(wmProductRecptLineService.save(wmProductRecptLine));
    }


    @PutMapping("/line/updateline")
    public DataResult updateline(@RequestBody WmProductRecptLine wmProductRecptLine){
        return DataResult.success(wmProductRecptLineService.updateById(wmProductRecptLine));
    }

    @DeleteMapping("/line/deleteline")
    public DataResult deleteline(@RequestBody List<Integer> logIds){
        return DataResult.success( wmProductRecptLineService.removeByIds(logIds));
    }
}
